using System;
using System.Linq;
using System.Collections.Generic;
using NUnit.Framework;
using Rhino.Mocks;
using WWB.SitePointPrivateMessages.Core.Infrastructure.Logging;
using WWB.SitePointPrivateMessages.Core.Model.DataImport;
using WWB.SitePointPrivateMessages.Core.Model.Messages;

namespace WWB.SitePointPrivateMessages.Tests.DataImportTests
{
    [TestFixture]
    public class ConvertFromFolderTests
    {
        private PrivateMessage pm1;
        private PrivateMessage pm2;
        private Folder sentItems;

        [SetUp]
        public void Arrange()
        {
            pm1 = new PrivateMessage()
            {
                DateStamp = DateTime.Parse("2006-11-14 01:24"),
                Title = "added iframe",
                FromUser = new ForumUser() { Id = 83449, Name = "SampleMember" },
                ToUsers = new List<ForumUser>() { new ForumUser() { Name = "FormAlly" } },
                Body =
                    @"If you can give me a link to the site I'll take a look at what's being added and look at what kind of user input stuff it has that should be checked. Not that I'm an expert, but it's worth a try.
SampleMember

"
            };

            pm2 = new PrivateMessage()
            {
                DateStamp = DateTime.Parse("2006-11-17 19:47"),
                Title = "Re: ideas",
                FromUser = new ForumUser() { Id = 83449, Name = "SampleMember" },
                ToUsers = new List<ForumUser>() { new ForumUser() { Name = "FormAlly" } },
                Body =
                    @"Good to hear you figured out what happened. And thanks for the link, interesting read. Now you just have to clean up the mess."
            };

            sentItems = new Folder("Sent Items", new []{pm1, pm2});
        }


        [Test]
        public void Folder_converts()
        {
            var exported = sentItems.ConvertToExport(MockRepository.GenerateStub<ILogger>());
            Assert.That(exported, Is.Not.Null);
        }

        [Test]
        public void Exported_has_correct_stuff()
        {
            var exported = sentItems.ConvertToExport(MockRepository.GenerateStub<ILogger>());
            exported.Messages[0].AssertPrivateMessageMatches(pm1);
            exported.Messages[1].AssertPrivateMessageMatches(pm2);
            Assert.That(exported.Messages.Count(), Is.EqualTo(2));
        }
    }

    [TestFixture]
    public class ConvertToFolderTests
    {
        private ExportedMessage m1;
        private ExportedMessage m2;
        private ExportedFolder sentItems;
        
        [SetUp]
        public void Arrange()
        {
            m1 = new ExportedMessage()
                     {
                         DateStamp = "2006-11-14 01:24",
                         Title = "added iframe",
                         FromUser = "SampleMember",
                         FromUserId = 83449,
                         ToUser = "FormAlly",
                         MessageBody =
                             @"If you can give me a link to the site I'll take a look at what's being added and look at what kind of user input stuff it has that should be checked. Not that I'm an expert, but it's worth a try.
SampleMember

"
                     };
            m2 = new ExportedMessage()
                     {
                         DateStamp = "2006-11-17 19:47",
                         Title = "Re: ideas",
                         FromUser = "SampleMember",
                         FromUserId = 83449,
                         ToUser = "FormAlly",
                         MessageBody = @"Good to hear you figured out what happened. And thanks for the link, interesting read. Now you just have to clean up the mess."
                     };
            sentItems = new ExportedFolder()
                            {
                                Name = "Sent Items",
                                Messages = new[] {m1, m2}
                            };
        }

        [Test]
        public void Pushes_correct_messages()
        {
            var f = sentItems.ConvertToFolder(new List<ForumUser>(), MockRepository.GenerateStub<ILogger>());
            Assert.That(f.Name, Is.EqualTo("Sent Items"));
            f.Messages[0].AssertPrivateMessageMatches(m1);
            f.Messages[1].AssertPrivateMessageMatches(m2);
            Assert.That(f.Messages.Count == 2);
        }
    }
}